<?php
namespace Lavender;

class Validator
{
	public static function is_mobile($mobile)
	{
		if (preg_match('/[^0-9\-\+]+/', $mobile)) {
			return false;
		}

		if (preg_match('/^((13[0-9])|(15[^4,\D])|(18[0-9]))\d{8}$/', $mobile)) {
			return true;
		}
		
		if (preg_match('/^(\+?(\(\d{2,3}\)\d{2,3}-?\d{4})|(\d{3}-\d{2,3}-\d{4})|(\d{10,11})|(\d{2,3}-\d{3,4}-\d{4}))$/', $mobile)) {
			return true;
		}
		
		return false;
	}

	public static function is_phone($number)
	{
//		$result = preg_match('/^(\+?[0-9]{2,3})?[0-9]{3,7}\-[0-9]{6,8}(\-[0-9]{2,6})?$/', $number);
		$result = preg_match('/^(((\d{3,5})-)?(\d{3,5}-)?\d{4,16})$/', $number);
		return $result ? true : false;
	}

	public static function is_email($email)
	{
		$result = filter_var($email, FILTER_VALIDATE_EMAIL);
		if ($result === false) {
			return false;
		}
		
		$result = preg_match('/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/', $email); //  /^\w+([-.]\w+)*@\w+([-]\w+)*([.]\w{2,})+$/
		return $result ? true : false;
	}

	/**
	 * �ж��Ƿ���cc�û�
	 */
	public static function is_cc_username($user_name)
	{
		$result = preg_match('/^[a-zA-Z][a-zA-Z0-9_\\.]{3,18}[a-zA-Z0-9_]$/', $user_name);
		return $result ? true : false;
	}

	public static function is_default_username($user_name)
	{
		$result = preg_match('/^(G|g)(L|l)_[1-9]+[0-9]*$/', $user_name);//^(g|G)(l|L)_\d+$
		return $result ? true : false;
	}

	// user_id 为int型 
	public static function is_cc_no($user_id)
	{
		$result = preg_match('/^[1-9]\d{0,9}$/', $user_id); // int
		return $result ? true : false;
	}

	/**
	 * �ж��Ƿ�������
	 */
	public static function is_positive($num)
	{
		$result = preg_match('/^[1-9]\d*$/', $num);
		return $result ? true : false;
	}

	public static function is_utf8_zh($str)
	{
		$result = preg_match('/^[\x{4e00}-\x{9fa5}]+$/u', $str);
		return $result ? true : false;
	}

	/**
	 * check is a valid url
	 *
	 * @param string $url
	 * @param array $domains in domain list
	 *
	 * @return boolean
	 */
	public static function is_url($url, $domains = null)
	{
		$ret = filter_var($url, FILTER_VALIDATE_URL);
		if ($ret === false) {
			return false;
		}

		if (empty($domains)) {
			return true;
		}

		//parse & check domain..

		$tmp = parse_url($url);

		//host not found
		if (empty($tmp['host'])) {
			return false;
		}

		$host = $tmp['host'];

		//fix host
		if (($pos = strpos($host, '#'))) {
			$host = substr($host, 0, $pos);
		}
		if (($pos = strpos($host, '?'))) {
			$host = substr($host, 0, $pos);
		}
		if (($pos = strpos($host, '\\'))) {
			$host = substr($host, 0, $pos);
		}

		//check domain
		foreach ($domains as $domain) {
			if ($domain == $host || substr($host, -(strlen($domain) + 1)) == '.' . $domain) {
				return true;
			}
		}

		return false;
	}

	public static function is_ip($ip)
	{
		$ret = filter_var($ip, FILTER_VALIDATE_IP);
		if ($ret === false) {
			return false;
		}

		return true;
	}

	public static function is_password($pw)
	{
		if(empty($pw)){
			return  false;
		}

		$len = strlen($pw);
		if ($len<6 || $len>20) {
			return false;
		}
		return true;
	}
}



